/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

Boxer - 框选工具
修改许可权限 - 江利峰

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function Boxer()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.util{
	import editor.common.Common;
	import editor.util.event.BoxerEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	
	public class Boxer extends Shape{
		
		public var isWorking:Boolean = false;
		
		private var _isMouseDown:Boolean;
		private var _isCtrlDown:Boolean;
		private var _container:DisplayObjectContainer;
		
		private var _startX:Number;
		private var _startY:Number;
		private var _endX:Number;
		private var _endY:Number;
		
		// 构造函数
		public function Boxer(container:DisplayObjectContainer)
		{
			_container = container;
			_container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_container.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			Common.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			Common.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function drawBox():void
		{
			this.graphics.clear();
			this.graphics.beginFill(0x0000ff, 0.2);
			this.graphics.lineStyle(1);
			this.graphics.moveTo(_startX, _startY);
			this.graphics.lineTo(_endX, _startY);
			this.graphics.lineTo(_endX, _endY);
			this.graphics.lineTo(_startX, _endY);
			this.graphics.lineTo(_startX, _startY);
			this.graphics.endFill();
		}
		
		//ef事件
		private function onEnterFrame(evt:Event):void
		{
			if(_isMouseDown)
			{
				_endX = mouseX;
				_endY = mouseY;
				
				drawBox();
			}
		}
		
		
		private function onKeyDown(evt:KeyboardEvent):void
		{
			if(evt.keyCode == Keyboard.CONTROL)
				_isCtrlDown = true;
		}
		
		private function onKeyUp(evt:KeyboardEvent):void
		{
			if(evt.keyCode == Keyboard.CONTROL)
				_isCtrlDown = false;
		}
		
		//鼠标按下
		private function onMouseDown(evt:MouseEvent):void
		{
			if(_isCtrlDown)
			{
				isWorking = true;
				_startX = mouseX;
				_startY = mouseY;
				_isMouseDown = true;
				addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		
		//鼠标抬起
		private function onMouseUp(evt:MouseEvent):void
		{
			if(isWorking)
			{
				isWorking = false;
				_isMouseDown = false;
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				this.graphics.clear();
				
				var be:BoxerEvent = new BoxerEvent(BoxerEvent.BOX_OVER);
				be.startPoint = new Point(_startX, _startY);
				be.endPoint = new Point(_endX, _endY);
				
				dispatchEvent(be);
			}
		}
		//
	}
}